/*
 * jira.h - cve-check-tool
 *
 * Copyright (C) 2015 Intel Corporation
 *
 * cve-check-tool is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#define _GNU_SOURCE

#pragma once

#include <glib.h>
#include <stdbool.h>

#include "cve-string.h"
#include "template.h"
#include "util.h"

/**
 * A JIRA issue
 */
struct jira_issue_t {
        gchar *key;         /**<Reader friendly key value of issue */
        gchar *status;      /**<Reader friendly name of issue */
        gchar *summary;     /**<Summary value of issue */
        gchar *description; /**<Description of issue */
        gchar *resolution;  /**<Resolution of issue if any */
};

/**
 * Adds a new JIRA issue with the given json string
 *
 * @param json The json string containing the JIRA insert syntax
 * @return true if successful
 */
bool add_new_jira_issue(const gchar *jira_json);

/**
 * Builds the json string to use for inserting a new bug into JIRA. Except
 * for summary and description, the remaining parameters are defined
 * under the JIRA-New-Issue section of the the cve-check-tool's
 * configuration file
 *
 * @param summary The CVE summary which is just the CVE ID
 * @param description The CVE description
 * @param as_template Set to true if you want to create just a template file
 * @param jira_json The resulting JIRA json string containing the insert syntax
 * @return true if successful
 */
bool build_new_jira_issue(const gchar *summary, const gchar *description, bool as_template, gchar **jira_json);

/**
 * Like build_new_jira_issue but loads and builds it from a json file.
 * The file can be a template or string literal. You must pass the summary
 * and description of the current CVE in queue to be added to JIRA. If the
 * file is a template then this is where the variable substitution takes
 * place. Except for summary and description any other variables must be
 * defined in the under the JIRA-New-Issue group in the cve-check-tool
 * configuration file.
 *
 * @param path Path to the json file
 * @param summary Summary of the current bug to add to JIRA
 * @param description Description of current bug to add to JIRA
 * @param jira_json The resulting json to send to JIRA
 * @return true if successful
 */
bool build_new_jira_issue_file(const gchar *path, const gchar *summary, const gchar *description, gchar **jira_json);

/**
 * Searches JIRA server for CVE related-issues
 *
 * @param jira_json The json sent to JIRA to conduct search. This string is
 *                  is created by parameters defined under the
 *                  JIRA-Search-Issues section of cve-check-tool's
 *                  configuration file.
 * @return true if successful
 */
bool build_search_jira_issues(gchar **jira_json);

/**
 * Checks contents of the a JIRA response for any error messages or if the
 * response is an empty or NULL string. Returns true if message looks good.
 *
 * @param jira_json JSON string containing response from server
 * @return true if successful
 */
bool check_jira_response(const gchar *jira_json);

/**
 * Frees internal plugin resources
 *
 * @return
 */
void destroy_jira_plugin(void);

/**
 * Helper function to frees resources for a jira_issues
 *
 * @param jira_issues A jira_issues list
 * @return
 */
void free_jira_issues(GSList **jira_issues);

/**
 * Searches a jira_issues list for any matches based upon "summary"
 *
 * @param jira_issues List of jira issues
 * @param summary The summary text of the jira issue to search
 * @return JIRA issue if found otherwise NULL
 */
struct jira_issue_t *get_jira_issue(const GSList *jira_issues, const gchar *summary);

/**
 * Searches a JIRA based upon parameters defined in cve-check-tool's
 * configuration JIRA-Search-Issues section
 *
 * @param jira_json The search json to send to JIRA server
 * @param jira_issues A list containing matches
 * @return true if successful
 */
bool get_jira_issues(const gchar *jira_json, GSList **jira_issues);

/**
 * Returns the total number of matched JIRA issues found in the given list
 *
 * @param jira_issues Matched JIRA issues as a list
 * @return Total number of issues. NULL also implies 0.
 */
int get_jira_issues_count(const GSList *jira_issues);

/**
 * Loads JIRA issues in json form from a file
 *
 * @param path Path to json file
 * @param jira_issues A list containing matches
 * @return true if successful
 */
bool get_jira_issues_file(const gchar *path, GSList **jira_issues);

/**
 * Initializes the plugin.  Run this before anything else. You
 * either pass in an already initialized config key_file or
 * set it NULL and provide a path to the cve-check-tool configuration
 * file instead.
 *
 * @param config An initialized key_file or NULL
 * @param path Path to cve-check-tool's configuration file or NULL
 * @return true if successful
 */
bool init_jira_plugin(GKeyFile *config, const gchar *path);

/**
 * Checks if JIRA server is minimally responsive. Note that this function
 * will not perform any kind of authentication with user name and password.
 * Instead this function is just checking if a simple response is returned
 * from the "url" specfified in the cve-check-tool's configuration file under
 * the JIRA section.
 *
 * @return true if successful
 */
bool is_jira_alive(void);

/**
 * Parses the json returned by JIRA during a search and looks for matches
 * based upon the search_filter value defined under the JIRA-Search-Issues
 * section of the the cve-check-tool configuration file.

 * @param jira_issues_json The json search results returned by the JIRA server
 * @param jira_issues A list of matched jira issues
 * @return true if successful
 */
bool parse_jira_issues(const gchar *jira_issues_json, GSList **jira_issues);

/**
 * A helper function to save some text to a file
 *
 * @param something Text to save to file
 * @param path Path to file
 * @return true if successful
 */
bool save(const gchar *something, const gchar *path);

/**
 * A helper function to save a JIRA issues list ti a file as CSV
 *
 * @param jira_issues JIRA issues list
 * @param path Path to file
 * @return true if successful
 */
bool save_jira_issues_csv(const GSList *jira_issues, const gchar *path);

/**
 * A helper function to save a JIRA issues list to a file as XML
 *
 * @param jira_issues JIRA issues list
 * @param path Path to file
 * @return true if successful
 */
bool save_jira_issues_xml(const GSList *jira_issues, const gchar *path);

/**
 * Searches JIRA for matching issues
 *
 * @param jira_json The json string to send to JIRA for query
 * @param path The returned json string from JIRA
 * @return true if successful
 */
bool search_jira_issues(const gchar *jira_json, gchar **jira_issues_json);

/**
 * Sets JIRA user password. Overrides configuration value setting
 *
 * @param password JIRA user password
 * @return
 */
void set_password(const gchar *password);

/**
 * Sets JIRA server timeout seconds. Overrides configuration value setting
 *
 * @param timeout_secs Timeout value in seconds
 * @return
 */
void set_timeout_secs(int timeout_secs);

/**
 * Sets JIRA REST API url. Overrides configuration value setting
 *
 * @param url JIRA rest url
 * @return
 */
void set_url(const gchar *url);

/**
 * Sets JIRA user name. Overrides configuration value setting
 *
 * @param user JIRA user name
 * @return
 */
void set_user(const gchar *user);

/**
 * Turns verbosity on or off. Overrides configuration value setting
 *
 * @param verbose_flag Set to true to turn on
 * @return
 */
void set_verbose(bool verbose_flag);

/**
 * Displays JIRA results from the given issues list
 *
 * @param jira_issues Matched JIRA issues list
 * @return
 */
void show_jira_issues(const GSList *jira_issues);

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 expandtab:
 * :indentSize=8:tabSize=8:noTabs=true:
 */
